package com.fowo.api.syncAmazon.util;

import org.threeten.bp.DateTimeUtils;
import org.threeten.bp.OffsetDateTime;
import org.threeten.bp.ZoneOffset;
import org.threeten.bp.ZonedDateTime;
import org.threeten.bp.format.DateTimeFormatter;
import org.threeten.bp.format.DateTimeParseException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;


public class AmzDateUtils {

	public static OffsetDateTime getOffsetDateTimeUTC(Calendar c) {
		return OffsetDateTime.of(c.get(Calendar.YEAR), c.get(Calendar.MONTH)+1,
				c.get(Calendar.DAY_OF_MONTH),c.get(Calendar.HOUR_OF_DAY),
				c.get(Calendar.MINUTE), c.get(Calendar.SECOND),
				c.get(Calendar.MILLISECOND), ZoneOffset.ofHours(8));
	}

	public static OffsetDateTime getOffsetDateTimeUTC(Date date) {
		 Calendar c=Calendar.getInstance();
		 c.setTime(date);
		return getOffsetDateTimeUTC(c);
	}

	public static Date getUTCToDate(OffsetDateTime date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
		sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
	    try {
			DateTimeFormatter df=DateTimeFormatter.ISO_INSTANT;
			return sdf.parse(date.format(df));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return null;
	}

	public static Date getDate(OffsetDateTime date) {
		ZoneOffset OFFSET_ID = ZoneOffset.of("+08:00");
		Calendar c=Calendar.getInstance();
		ZonedDateTime zonetime = date.atZoneSameInstant(OFFSET_ID);
	    c.set(zonetime.getYear(), zonetime.getMonth().getValue()-1, zonetime.getDayOfMonth(), zonetime.getHour(), zonetime.getMinute(), zonetime.getSecond());
	    return c.getTime();
	}


	public static String  getDateToUTCStr(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
		sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
	    return sdf.format(date);
	}


	public static LocalDateTime getLocalTime(String date) {
		// TODO Auto-generated method stub
		if(date==null)return null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
		sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
		Date time = null;
		try {
			  time = sdf.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(time!=null) {
			return getLocalTime(time);
		}else {
		   return	null;
		}
	}

	public static org.threeten.bp.LocalDateTime strToLocalTime(String dateStr){
		DateTimeFormatter[] formatters = {
				DateTimeFormatter.ofPattern("yyyy-MM-dd"),
				DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
		};

		for (DateTimeFormatter formatter : formatters) {
			try {
				org.threeten.bp.LocalDate localDate = org.threeten.bp.LocalDate.parse(dateStr, formatter);
				return localDate.atStartOfDay();

			} catch (DateTimeParseException e) {
				// 如果当前格式不匹配，则继续尝试下一个格式
				continue;
			}
		}
		throw new IllegalArgumentException("无法解析输入的日期字符串: " + dateStr);
	}

	public static Date strToDate(String date) {
		org.threeten.bp.LocalDateTime localDateTime = strToLocalTime(date);
		return localDateTimeToDate(localDateTime);
	}

	/**
	 * 置为当天零点
	 * @param date 传入参数
	 * @return
	 */
	public static Date getZeroOfDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 设置时间为0点（即清空小时、分钟、秒和毫秒）
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);

		return calendar.getTime();
	}

	public static Date getZeroOfDay(LocalDateTime dateTime){
		Date date = localDateTimeToDate(dateTime);
		return getZeroOfDay(date);
	}

	public static Date localDateTimeToDate(LocalDateTime date) {
		if(date==null)return null;
		Instant instant = date.atZone(ZoneId.systemDefault()).toInstant();
		return Date.from(instant);
	}

	public static Date localDateTimeToDate(org.threeten.bp.LocalDateTime date){
		if (date==null)return null;
		org.threeten.bp.Instant instant = date.atZone(org.threeten.bp.ZoneId.systemDefault()).toInstant();
		return DateTimeUtils.toDate(instant);
	}

	public static LocalDateTime getLocalTime(Date postdate) {
		// TODO Auto-generated method stub
		Calendar c=Calendar.getInstance();
		c.setTime(postdate);
		return LocalDateTime.of(c.get(Calendar.YEAR), c.get(Calendar.MONTH)+1, c.get(Calendar.DAY_OF_MONTH),
				c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), c.get(Calendar.SECOND));
	}

	public static LocalDate getLocalDate(Date postdate) {
		// TODO Auto-generated method stub
		if(postdate==null)return null;
		Calendar c=Calendar.getInstance();
		c.setTime(postdate);
		return LocalDate.of(c.get(Calendar.YEAR), c.get(Calendar.MONTH)+1, c.get(Calendar.DAY_OF_MONTH));
	}

	public static Date getDate(LocalDateTime date) {
		if(date==null) {
			return null;
		}
	    Calendar c=Calendar.getInstance();
	    c.set(date.getYear(), date.getMonth().getValue()-1, date.getDayOfMonth(), date.getHour(), date.getMinute(), date.getSecond());
	    return c.getTime();
	}

	public static Date getDatePlus(String date) {
		if(null == date){
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		try {
			Calendar c = Calendar.getInstance();
			String date2 = date.substring(0, 19);
			if (date2 != null) {
				c.setTime(sdf.parse(date2.trim()));
//                c.add(Calendar.HOUR, 8);
			}
			return c.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getReasonText(String Reason){
		String reasonText = "";
		switch (Reason){
			case "6":
			case "7":
			case "E":
			case "H":
			case "K":
			case "U":
				reasonText = "Damaged at Amazon fulfillment center";
				break;
			case "D":
			case "G":
				reasonText = "Inventory disposed of";
				break;
			case "F":
			case "N":
				reasonText = "Inventory found";
				break;
			case "M":
			case "5":
				reasonText = "Inventory misplaced";
				break;
			case "3":
				reasonText = "Product redefinition transfer in";
				break;
			case "4":
				reasonText = "Product redefinition transfer out";
				break;
			case "O":
				reasonText = "Inventory correction";
				break;
			case "P":
			case "Q":
				reasonText = "Inventory disposition change";
				break;
			default:
				break;
		}

		return reasonText;
	}
}
